package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.limao.DBDataClass.IpDateClass;
import com.limao.DbConnection.DbConnectionManager;


public class IpProvider {

	private static class DB_IpProvider {
		private static IpProvider instance = new IpProvider();
	}

	public static IpProvider getInstance() {
		return DB_IpProvider.instance;
	}
	
	
	public IpProvider(){}
	
	public ArrayList<IpDateClass> Load_Ip_All() {
		
		ArrayList<IpDateClass> list = new ArrayList<IpDateClass>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("SELECT * FROM cd_sys_ip");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				IpDateClass obj = new IpDateClass();
				obj.ip = rs.getString("ip");// ID
				obj.stime = rs.getString("stime") ;
				obj.etime = rs.getString("etime");
				obj.reg = rs.getString("reg");
				obj.auth = rs.getString("auth");
				obj.rmb = rs.getString("rmb");
				list.add(obj);
			}
		} catch (Exception e) {
			list=null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return list;

	}
	
	public IpDateClass Load_Ip(String ip) {
		
		IpDateClass obj = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("SELECT * FROM cd_sys_ip where ip=?");
			pstmt.setString(1, ip);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				obj = new IpDateClass();
				obj.ip = rs.getString("ip");// ID
				obj.stime = rs.getString("stime") ;
				obj.etime = rs.getString("etime");	
				obj.reg = rs.getString("reg");
				obj.auth = rs.getString("auth");
				obj.rmb = rs.getString("rmb");
			}
		} catch (Exception e) {
			obj=null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return obj;

	}
	
	
	public void delete_Ip(String ip) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("delete FROM cd_sys_ip where ip=?");
			pstmt.setString(1, ip);
			pstmt.execute();
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

	}	
	
	
}
